package com.example.weight.navigation;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.weight.R;
import com.example.weight.config.Config;
import com.example.weight.wormHandle.wormHandleNav;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

import org.json.JSONException;
import org.json.JSONObject;

public class MainNavigationActivity extends AppCompatActivity {

    private Button householdWasteNav, kitchenRubbishNav, sijiqingCenterNav,sijiqingCenterNav_old;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_navigation);

        Intent previousIntent = getIntent();
        final String name = previousIntent.getStringExtra("name");
        final String factory = previousIntent.getStringExtra("factory");
        // 下面一行完成后取消注释
        String permissionLevel = previousIntent.getStringExtra("permissionLevel");



        TitleBar titleBar = findViewById(R.id.main_navigation_titleBar);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {}

            @Override
            public void onRightClick(View v) {}
        });

        householdWasteNav = findViewById(R.id.household_waste_nav);
        kitchenRubbishNav = findViewById(R.id.kitchen_rubbish_nav);
        sijiqingCenterNav = findViewById(R.id.sijiqing_center_nav);
        sijiqingCenterNav_old = findViewById(R.id.sijiqing_center_nav_old);

        initNavEnable(permissionLevel);

        householdWasteNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainNavigationActivity.this, HouseholdWasteNavActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("factory", factory);
                startActivity(intent);
            }
        });

        kitchenRubbishNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainNavigationActivity.this, wormHandleNav.class);
                intent.putExtra("name", name);
                Log.d("mainnavi",factory);
                intent.putExtra("factory", factory);
                // 完成后取消注释
                if (permissionLevel.equals("0"))
                    intent.putExtra("permissionLevel", "0");
                else
                    intent.putExtra("permissionLevel", permissionLevel);
                startActivity(intent);
            }
        });

        sijiqingCenterNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainNavigationActivity.this, YoujiziyuanzaishengCenterNavigationActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("factory", factory);
                // 完成后取消注释
                if (permissionLevel.equals("0"))
                    intent.putExtra("permissionLevel", "0");
                else
                    intent.putExtra("permissionLevel", permissionLevel);
                Log.d("mainnavi", factory);
                startActivity(intent);
            }
        });
        sijiqingCenterNav_old.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MainNavigationActivity.this, SijiqingCenterNavActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("factory", factory);
                // 完成后取消注释
                if (permissionLevel.equals("0"))
                    intent.putExtra("permissionLevel", "0");
                else
                    intent.putExtra("permissionLevel", permissionLevel);
                Log.d("mainnavi", factory);
                startActivity(intent);
            }
        });
    }

    private void initNavEnable(String permissionLevel) {
        if (permissionLevel.startsWith("0"))
            return;
        householdWasteNav.setEnabled(false);
        kitchenRubbishNav.setEnabled(false);
        sijiqingCenterNav.setEnabled(false);
        sijiqingCenterNav_old.setEnabled(false);
        if (permissionLevel.contains("生活垃圾")) {
            // 生活垃圾相关人员
            householdWasteNav.setEnabled(true);
        }
        if (permissionLevel.contains("黑水")) {
            // 厨余垃圾相关人员
            kitchenRubbishNav.setEnabled(true);
        }
        if (permissionLevel.contains("四季青")) {
            // 四季青相关人员
            sijiqingCenterNav_old.setEnabled(true);
        }
        if (permissionLevel.contains("生产现场")||permissionLevel.contains("工艺现场")) {
            sijiqingCenterNav.setEnabled(true);
        }
    }

    private void getHouseHoldWasteNotice(String functionalUrl, int totalMaintenanceNum) {
            RequestQueue queue = Volley.newRequestQueue(this);
            String url = Config.url + functionalUrl + "/getInThisWeek";
            StringRequest setSysNameRequest = new StringRequest(Request.Method.GET, url,
                    new com.android.volley.Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                if (!jsonObject.isNull("data")) {
                                    int actualNum = jsonObject.getInt("data");
                                    if (totalMaintenanceNum > actualNum) {
                                        new AlertDialog.Builder(MainNavigationActivity.this)
                                                .setCancelable(false)
                                                .setMessage("您还有 "+(totalMaintenanceNum-actualNum)+" 个保养内容未处理")
                                                .setPositiveButton("确定", null)
                                                .show();
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainNavigationActivity.this,
                            "获取信息失败 " + error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
            setSysNameRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            queue.add(setSysNameRequest);
    }

    private void getSJQNotice(String functionalUrl, int dailyExpect, int weekExpect, int monthExpect) {
        RequestQueue queue = Volley.newRequestQueue(this);
        String url = Config.url + functionalUrl + "/getMaintenanceNum";
        StringRequest setSysNameRequest = new StringRequest(Request.Method.GET, url,
                new com.android.volley.Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            if (!jsonObject.isNull("data")) {
                                JSONObject data = jsonObject.getJSONObject("data");
                                int dailyNum = data.getInt("daily");
                                int weekNum = data.getInt("week");
                                int monthNum = data.getInt("month");
                                if (dailyNum < dailyExpect || weekNum < weekExpect || monthNum < monthExpect) {
                                    new AlertDialog.Builder(MainNavigationActivity.this)
                                            .setCancelable(false)
                                            .setMessage("您还有：\n"+
                                                        (dailyExpect-dailyNum)+" 个日保养内容未处理\n"+
                                                        (weekExpect-weekNum)+" 个周保养内容未处理\n"+
                                                        (monthExpect-monthNum)+" 个月保养内容未处理")
                                            .setPositiveButton("确定", null)
                                            .show();
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainNavigationActivity.this,
                        "获取信息失败 " + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        setSysNameRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(setSysNameRequest);
    }
}
